﻿Imports System.Collections.Generic

Imports Microsoft.VisualStudio.TestTools.UnitTesting

Imports QBuilder



'''<summary>
'''This is a test class for JoinInfoTest and is intended
'''to contain all JoinInfoTest Unit Tests
'''</summary>
<TestClass()> _
Public Class JoinInfoTest


    Private testContextInstance As TestContext

    '''<summary>
    '''Gets or sets the test context which provides
    '''information about and functionality for the current test run.
    '''</summary>
    Public Property TestContext() As TestContext
        Get
            Return testContextInstance
        End Get
        Set(value As TestContext)
            testContextInstance = Value
        End Set
    End Property

    '''<summary>
    '''A test for JoinInfo Constructor
    '''</summary>
    <TestMethod()> _
    Public Sub JoinInfoConstructorTest()
        Dim Table As String = "TableA"
        Dim LeftExpression As String = "Field1"
        Dim CriteriaOp As Operation = Operation.EqualTo
        Dim RightExpression As String = "Field2"
        Dim target As JoinInfo = New JoinInfo(Table, LeftExpression, CriteriaOp, RightExpression)
        Assert.AreEqual("INNER JOIN TableA ON Field1 = Field2", target.GetText)
    End Sub

    '''<summary>
    '''A test for JoinInfo Constructor
    '''</summary>
    <TestMethod()> _
    Public Sub JoinInfoConstructorTest1()
        Dim Table As String = "TableA"
        Dim target As JoinInfo = New JoinInfo(Table)
        Assert.AreEqual("TableA", target.Table)
    End Sub

    '''<summary>
    '''A test for JoinInfo Constructor
    '''</summary>
    <TestMethod()> _
    Public Sub JoinInfoConstructorTest2()
        Dim target As JoinInfo = New JoinInfo()
        Assert.IsNull(target.Table)
    End Sub

    '''<summary>
    '''A test for AddCriteria
    '''</summary>
    <TestMethod()> _
    Public Sub AddCriteriaTest()
        Dim target As JoinInfo = New JoinInfo()
        Dim LeftExpression As String = "FieldA"
        Dim CriteriaOp As Operation = Operation.EqualTo
        Dim RightExpression As String = "FieldB"
        Dim expected As New Criteria(LeftExpression, CriteriaOp, RightExpression)
        expected.RightExpressionType = ExpressionType.Logical
        Dim actual As Criteria
        actual = target.AddCriteria(LeftExpression, CriteriaOp, RightExpression)
        Assert.AreEqual(expected.GetText, actual.GetText)
    End Sub

    '''<summary>
    '''A test for AddCriteria
    '''</summary>
    <TestMethod()> _
    Public Sub AddCriteriaTest1()
        Dim target As ICriteriaContainer = New JoinInfo()
        Dim LeftExpression As String = "FieldA"
        Dim CriteriaOp As Operation = Operation.EqualTo
        Dim RightExpression As String = "FieldB"
        Dim Op As BooleanOperator = New BooleanOperator()
        Dim expected As New Criteria(LeftExpression, CriteriaOp, RightExpression)
        expected.RightExpressionType = ExpressionType.Logical

        Dim actual As Criteria
        actual = target.AddCriteria(LeftExpression, CriteriaOp, RightExpression, Op)
        Assert.AreEqual(expected.GetText, actual.GetText)
    End Sub

    '''<summary>
    '''A test for AddCriteria
    '''</summary>
    <TestMethod()> _
    Public Sub AddCriteriaTest2()
        Dim target As ICriteriaContainer = New JoinInfo()
        Dim Criteria As ICriteriaComponent = New Criteria
        target.AddCriteria(Criteria)
        Assert.AreEqual(1, target.CriteriaCount)
    End Sub

    '''<summary>
    '''A test for AddCriteria
    '''</summary>
    <TestMethod()> _
    Public Sub AddCriteriaTest3()
        Dim target As ICriteriaContainer = New JoinInfo()
        Dim Criteria As ICriteriaComponent = New Criteria
        Dim Op As BooleanOperator = BooleanOperator.Default
        target.AddCriteria(Criteria, Op)
        Assert.AreEqual(1, target.CriteriaCount)
    End Sub

    '''<summary>
    '''A test for ClearCriteria
    '''</summary>
    <TestMethod()> _
    Public Sub ClearCriteriaTest()
        Dim target As ICriteriaContainer = New JoinInfo()
        target.AddCriteria(New Criteria)
        target.ClearCriteria()
        Assert.AreEqual(0, target.CriteriaCount)
    End Sub

    '''<summary>
    '''A test for Criteria
    '''</summary>
    <TestMethod()> _
    Public Sub CriteriaTest()
        Dim target As ICriteriaContainer = New JoinInfo()
        Dim crit1 As Criteria = target.AddCriteria("", Operation.EqualTo, "")
        Dim crit2 As Criteria = target.AddCriteria("", Operation.EqualTo, "")

        Dim enumerator As IEnumerator(Of ICriteriaComponent) = target.Criteria.GetEnumerator

        enumerator.MoveNext()
        Assert.AreEqual(crit1, enumerator.Current)
        enumerator.MoveNext()
        Assert.AreEqual(crit2, enumerator.Current)
    End Sub

    '''<summary>
    '''A test for GetJoinTypeText
    '''</summary>
    <TestMethod()>
    Public Sub GetJoinTypeTextTest()
        Dim Table As String = "TableA"
        Dim LeftExpression As String = "Field1"
        Dim CriteriaOp As Operation = Operation.EqualTo
        Dim RightExpression As String = "Field2"
        Dim target As JoinInfo = New JoinInfo(Table, LeftExpression, CriteriaOp, RightExpression)
        target.JoinType = JoinType.Cross
        Assert.AreEqual("CROSS JOIN TableA ON Field1 = Field2", target.GetText)
        target.JoinType = JoinType.FullOutter
        Assert.AreEqual("OUTTER JOIN TableA ON Field1 = Field2", target.GetText)
        target.JoinType = JoinType.Inner
        Assert.AreEqual("INNER JOIN TableA ON Field1 = Field2", target.GetText)
        target.JoinType = JoinType.LeftOutter
        Assert.AreEqual("LEFT JOIN TableA ON Field1 = Field2", target.GetText)
        target.JoinType = JoinType.RightOutter
        Assert.AreEqual("RIGHT JOIN TableA ON Field1 = Field2", target.GetText)
    End Sub

    '''<summary>
    '''A test for GetJoinTypeText
    '''</summary>
    <TestMethod()>
    <ExpectedException(GetType(InvalidOperationException))>
    Public Sub GetJoinTypeTextTest_InvalidJoinType()
        Dim Table As String = "TableA"
        Dim LeftExpression As String = "Field1"
        Dim CriteriaOp As Operation = Operation.EqualTo
        Dim RightExpression As String = "Field2"
        Dim target As JoinInfo = New JoinInfo(Table, LeftExpression, CriteriaOp, RightExpression)
        target.JoinType = CType(100, JoinType) 'Some invalid number
        Assert.AreEqual("RIGHT JOIN TableA ON Field1 = Field2", target.GetText)
    End Sub

    '''<summary>
    '''A test for GetText
    '''</summary>
    <TestMethod()> _
    Public Sub GetTextTest()
        Dim Table As String = "TableA"
        Dim LeftExpression As String = "Field1"
        Dim CriteriaOp As Operation = Operation.EqualTo
        Dim RightExpression As String = "Field2"
        Dim target As JoinInfo = New JoinInfo(Table, LeftExpression, CriteriaOp, RightExpression)
        Assert.AreEqual("INNER JOIN TableA ON Field1 = Field2", target.GetText)
    End Sub

    '''<summary>
    '''A test for GetText
    '''</summary>
    <TestMethod()> _
    Public Sub GetTextTest1()
        Dim Table As String = "TableA"
        Dim LeftExpression As String = "Field1"
        Dim CriteriaOp As Operation = Operation.EqualTo
        Dim RightExpression As String = "Field2"
        Dim target As JoinInfo = New JoinInfo(Table, LeftExpression, CriteriaOp, RightExpression)
        Assert.AreEqual("INNER JOIN TableA ON Field1 = Field2", target.GetText(QueryOptions.DefaultOptions))
    End Sub

    '''<summary>
    '''A test for CriteriaCount
    '''</summary>
    <TestMethod()> _
    Public Sub CriteriaCountTest()
        Dim target As ICriteriaContainer = New JoinInfo()
        target.AddCriteria(New Criteria)
        target.AddCriteria(New Criteria)
        target.AddCriteria(New Criteria)

        Dim actual As Integer
        actual = target.CriteriaCount
        Assert.AreEqual(3, actual)
    End Sub

    '''<summary>
    '''A test for JoinType
    '''</summary>
    <TestMethod()> _
    Public Sub JoinTypeTest()
        Dim target As JoinInfo = New JoinInfo()
        Dim expected As JoinType = JoinType.Cross
        Dim actual As JoinType
        target.JoinType = expected
        actual = target.JoinType
        Assert.AreEqual(expected, actual)
    End Sub

    '''<summary>
    '''A test for RenderParens
    '''</summary>
    <TestMethod()> _
    Public Sub RenderParensTest()
        Dim target As ICriteriaContainer = New JoinInfo()
        Dim expected As Boolean = False
        Dim actual As Boolean
        target.RenderParens = True
        actual = target.RenderParens
        Assert.AreEqual(expected, actual)
    End Sub

    '''<summary>
    '''A test for Table
    '''</summary>
    <TestMethod()> _
    Public Sub TableTest()
        Dim target As JoinInfo = New JoinInfo()
        Dim expected As String = "TableA"
        Dim actual As String
        target.Table = expected
        actual = target.Table
        Assert.AreEqual(expected, actual)
    End Sub
End Class
